package com.dc.project.controller.user;

import com.dc.project.constant.ControllerConstant;
import com.dc.project.entity.dto.TaskDto;
import com.dc.project.entity.form.BaseCreateTaskForm;
import com.dc.project.entity.form.ClearTaskPasswordForm;
import com.dc.project.entity.form.CreateTaskForm;
import com.dc.project.entity.form.UpdateTaskPasswordForm;
import com.dc.project.entity.query.TaskQuery;
import com.dc.project.entity.vo.TaskVo;
import com.dc.project.service.TaskService;
import com.dc.project.util.TaskPasswordUtil;
import com.own.aop.login.LoginIgnore;
import com.own.aop.operation.LogOperationModuleAop;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.common.model.entity.ContentModel;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.exception.BusinessSimpleException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserClientTaskController
 *
 * @author chenxueli
 * @date 2025-03-08 15:11:52
 */
@Slf4j
@Tag(name = "任务" + ConstantBeanSuffix.CONTROLLER)
@RestController
@LoginIgnore
@CrossOrigin("*")
@LogOperationModuleAop("任务")
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ControllerConstant.PREFIX_USER_API + "task")
public class UserClientTaskController extends BaseController {

    @Resource
    private TaskService taskService;

    @Resource
    private TaskPasswordUtil taskPasswordUtil;

    @Operation(summary = "查询出任务列表的信息（分页查询）")
    @PostMapping("/page")
    public ResultModel<PageModel<TaskVo>> page(
            @RequestBody TaskQuery query
    ) {
        var pageModel = taskService.page(query);
        return R.success(pageModel);
    }

    @Operation(summary = "修改任务名称")
    @PutMapping("/title/{taskId}")
    public ResultModel<Boolean> updateTitle(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId,
            @RequestBody TaskDto dto
    ) {
        if (StringUtils.isBlank(dto.getTitle())) {
            throw new BusinessSimpleException("任务名称不能为空");
        }
        taskService.updateTitle(taskId, dto.getTitle());
        return R.success(true);
    }

    @Operation(summary = "根据文件列表创建任务")
    @PostMapping("/create/by/files")
    public ResultModel<TaskVo> createTaskByFiles(
            @RequestBody CreateTaskForm form
    ) {
        form.check();
        var vo = taskService.createByFileList(form.getCompanyName(), form.getFileIdList());
        return R.success(vo);
    }

    @Operation(summary = "创建《高新技术企业申报材料清单及要求》的任务")
    @PostMapping("/create/high/tech/enterprise")
    public ResultModel<TaskVo> createHighTechEnterprise(
            @RequestBody BaseCreateTaskForm form
    ) {
        form.check();
        var vo = taskService.createByTemplateKey(form.getCompanyName(), form.getRealmName(), "high_tech_enterprise");
        return R.success(vo);
    }

    @Operation(summary = "根据id获取信息")
    @GetMapping("/{taskId}")
    public ResultModel<TaskVo> getById(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId
    ) {
        var vo = taskService.getById(taskId);
        // 验证密码是否正确
        if (vo.getIsEncrypt() != null && vo.getIsEncrypt() == 1) {
            taskPasswordUtil.check(vo.getPassword());
        }
        return R.success(vo);
    }

    @Operation(summary = "根据任务id设置热门")
    @PutMapping("/change/hot/{taskId}")
    public ResultModel<Boolean> changeHot(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId
    ) {
        taskService.changeHot(taskId);
        return R.success(true);
    }

    @Operation(summary = "根据任务id删除任务")
    @DeleteMapping("/{taskId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId
    ) {
        taskService.delete(taskId);
        return R.success(true);
    }

    @Operation(summary = "修改领域名称")
    @PutMapping("/realm/{taskId}")
    public ResultModel<Boolean> updateRealmName(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId,
            @Parameter(description = "领域名称", required = true) @RequestParam(required = true) String realmName
    ) {
        taskService.updateRealmName(taskId, realmName);
        return R.success(true);
    }

    @Operation(summary = "查询是否是加密数据")
    @GetMapping("/check/encrypt/{taskId}")
    public ResultModel<Boolean> checkIsEncrypt(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId
    ) {
        var po = taskService.getPoById(taskId, false);
        return R.success(po != null && po.getIsEncrypt() == 1);
    }

    @Operation(summary = "修改任务密码")
    @PutMapping("/password/{taskId}")
    public ResultModel<Boolean> updatePassword(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId,
            @RequestBody UpdateTaskPasswordForm form
    ) {
        taskService.updatePassword(taskId, form.getOldPassword(), form.getNewPassword());
        return R.success(true);
    }

    @Operation(summary = "清除任务密码")
    @PutMapping("/clear/password/{taskId}")
    public ResultModel<Boolean> clearPassword(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId,
            @RequestBody ClearTaskPasswordForm form
    ) {
        taskService.clearPassword(taskId, form.getPassword());
        return R.success(true);
    }

    @Operation(summary = "修改任务提示词")
    @PutMapping("/prompt/{taskId}")
    public ResultModel<Boolean> updatePrompt(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId,
            @RequestBody ContentModel model
    ) {
        model.check();
        taskService.updatePromptByTaskId(taskId, model.getContent());
        return R.success(true);
    }

}
